package cn.h8k.hj212.codec.impl;

import cn.h8k.hj212.codec.Hj212Decoder;
import cn.h8k.hj212.constant.Hj212Constants;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * HJ212协议解码器默认实现
 * 可被继承以自定义解码行为
 */
public class DefaultHj212Decoder implements Hj212Decoder {
    
    /**
     * 将HJ212协议字符串转换为数据字典
     * @param data HJ212协议字符串
     * @return 解析后的数据字典
     */
    @Override
    public Map<String, Object> decode(String data) {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put(Hj212Constants.HEAD, data.substring(0, 2));
        result.put(Hj212Constants.LENGTH, data.substring(2, 6));
        result.put(Hj212Constants.CRC, data.substring(data.length() - 6, data.length() - 2));
        
        Map<String, Object> dataMap = new LinkedHashMap<>();
        
        String body = data.substring(6, data.length() - 6);
        String[] parts = body.split(Hj212Constants.GROUP_SEPARATOR);
        if (parts.length > 1) {
            // 处理非CP部分
            String mainPart = parts[0];
            String[] mainItems = mainPart.split(Hj212Constants.FIELD_SEPARATOR);
            for (String item : mainItems) {
                if (item.isEmpty() || !item.contains(Hj212Constants.KEY_VALUE_SEPARATOR)) continue;
                String[] kv = item.split(Hj212Constants.KEY_VALUE_SEPARATOR, 2);
                if (kv.length == 2) {
                    dataMap.put(kv[0], kv[1]);
                }
            }
            // 处理CP部分
            Map<Integer, Map<String, Object>> cpMap = decodeCpSection(parts);
            dataMap.put(Hj212Constants.CP, cpMap);
        }
        result.put(Hj212Constants.DATA, dataMap);
        return result;
    }
    
    /**
     * 保护方法：子类可重写以自定义CP部分的解码
     * @param parts 分割后的协议部分
     * @return 解析后的CP映射
     */
    protected Map<Integer, Map<String, Object>> decodeCpSection(String[] parts) {
        Map<Integer, Map<String, Object>> cpMap = new LinkedHashMap<>();
        
        if (parts.length > 1 && parts[1].length() > 0) {
            String cpPart = parts[1];
            String[] cpGroups = cpPart.split(Hj212Constants.FIELD_SEPARATOR);
            
            for (int i = 0; i < cpGroups.length; i++) {
                String cpGroup = cpGroups[i];
                if (cpGroup.isEmpty()) continue;
                
                Map<String, Object> cpItemMap = decodeCpGroup(cpGroup);
                if (!cpItemMap.isEmpty()) {
                    cpMap.put(i, cpItemMap);
                }
            }
        }
        
        return cpMap;
    }
    
    /**
     * 保护方法：子类可重写以自定义CP组的解码
     * @param cpGroup CP组字符串
     * @return 解析后的CP项映射
     */
    protected Map<String, Object> decodeCpGroup(String cpGroup) {
        Map<String, Object> cpItemMap = new LinkedHashMap<>();
        String[] cpItems = cpGroup.split(Hj212Constants.ITEM_SEPARATOR);
        
        for (String cpItem : cpItems) {
            if (cpItem.isEmpty() || !cpItem.contains(Hj212Constants.KEY_VALUE_SEPARATOR)) continue;
            
            String[] kv = cpItem.split(Hj212Constants.KEY_VALUE_SEPARATOR, 2);
            if (kv.length == 2) {
                Object value = parseValue(kv[1]);
                cpItemMap.put(kv[0], value);
            }
        }
        
        return cpItemMap;
    }
    
    /**
     * 保护方法：子类可重写以自定义值的解析
     * @param valueStr 值字符串
     * @return 解析后的值对象
     */
    protected Object parseValue(String valueStr) {
        try {
            if (valueStr.contains(Hj212Constants.DECIMAL_POINT)) {
                return Double.parseDouble(valueStr);
            } else {
                return valueStr;
            }
        } catch (NumberFormatException e) {
            return valueStr;
        }
    }
} 